<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\VoteOptions;
use App\Models\Vote;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Str;

class VoteOptionsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VoteOptions(),function(Grid $grid) {
            // 禁止
            $grid->toolsWithOutline(false);

            // 设置初始排序条件
            $grid->model()->orderBy('sort_id','asc');

            $grid->model()->orderBy('number_id','asc');

            $grid->model()->orderBy('id','desc');

            $grid->model()->orderBy('updated_at','desc');

            $grid->setActionClass(Grid\Displayers\Actions::class);

            $grid->column('sort_id')->editable(true);

            // 关联 产品分类 表数据
            $grid->model()->with(['vote']);

            $grid->column('vote.title','对应投票');

            $grid->column('title')->editable(true);

            $grid->column('number_id')->editable(true);

            $grid->column('thumbnail')->display(function($pictures) {

                if(empty($pictures)) {
                    $pictures = admin_url() . '/../themes/not.jpg';
                }
                else {

                    $pictures = admin_url() . '/../../../storage/app/public' . $pictures;
                }

                return $pictures;

            })->image();

            $grid->column('is_using','状态')->switch('',true);

            $grid->column('created_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->column('updated_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->filter(function(Grid\Filter $filter) {

                // 下拉菜单
                $array = Vote::getClsSelect();

                $filter->equal('vote_id','所属投票')->select($array);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id,new VoteOptions(),function(Show $show) {
            $show->field('id');
            $show->field('vote_id');
            $show->field('options_id');
            $show->field('title');
            $show->field('sort_id');
            $show->field('number_id');
            $show->field('accumulate_id');
            $show->field('visits_id');
            $show->field('content');
            $show->field('introduction');
            $show->field('thumbnail');
            $show->field('images');
            $show->field('qr_code');
            $show->field('is_using');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VoteOptions(),function(Form $form) {

            $form->disableResetButton();

            $form->disableEditingCheck();

            $form->disableViewCheck();

            $form->hidden('options_id');

            // 分类
            $array = Vote::getClsSelect();

            $form->select('vote_id')->placeholder('请选择投票ID！')->options($array)->required('min:1');

            $form->text('title')->required('min:1');

            $form->number('sort_id');

            $form->number('number_id')->help('此配置项的序号');

            $form->number('accumulate_id');

            $form->number('visits_id')->help('有人访问，就会增加！')->default(1);

            $form->editor('content')->height(800)->required('min:1');

            $form->textarea('introduction')->required('min:1');

            // 缩略图
            $form->image('thumbnail')
                ->uniqueName()
                ->autoUpload()
                ->disk('public')
                ->move('/images/vote')
                ->maxSize(getConf('IMAGE_UPLOAD_SIZE'))
                ->accept(getConf('IMAGE_UPLOAD_TYPE'));

            // 图集
            $form->multipleImage('images')
                ->uniqueName()
                ->autoUpload()
                ->disk('public')
                ->move('/images/vote')
                ->maxSize(getConf('IMAGE_UPLOAD_SIZE'))
                ->accept(getConf('IMAGE_UPLOAD_TYPE'))
                ->limit(3)
                ->help('最多可以上传3张图片！')->saving(function($value) {
                    // 转化成json字符串保存到数据库
                    return json_encode($value);
                });

            // 缩略图
            $form->image('qr_code')
                ->uniqueName()
                ->autoUpload()
                ->disk('public')
                ->move('/images/vote')
                ->maxSize(getConf('IMAGE_UPLOAD_SIZE'))
                ->accept(getConf('IMAGE_UPLOAD_TYPE'));

            $form->radio('is_using')->options([1 => '开启',0 => '禁用'])->default(1)->required('min:1');

            // 保存时
            $form->saving(function(Form $form) {
                $form->options_id = Str::uuid();
            });
        });
    }
}
